#CFLAGS=-I`$(OCAMLC) -where` -I$(MPIINCDIR) -O -g -Wall
SHELL = /bin/sh
VPATH = @srcdir@

.PHONY: clean distclean

subdirs = @subdirs@
top_srcdir = @top_srcdir@
srcdir = @srcdir@
prefix = @prefix@
exec_prefix = @exec_prefix@
bindir = $(exec_prefix)/bin
infodir = $(prefix)/info
libdir = $(prefix)/lib/gnudl
mandir = $(prefix)/man/man1

CC = @CC@
CPPFLAGS = @CPPFLAGS@
CFLAGS = $(CPPFLAGS) @CFLAGS@ 
LDFLAGS = @LDFLAGS@
LIBS = @LIBS@
INSTALL = @INSTALL@

USEPARALLEL := @USEPARALLEL@
USEINTERFACE := @USEINTERFACE@
USEGRAPHICS := @USEGRAPHICS@
USESHAREDHD := @USESHAREDHD@
OCAML := @OCAML@
OCAMLC := @OCAMLC@
OCAMLOPT := @OCAMLOPT@
OCAMLDEP := @OCAMLDEP@
OCAMLTOP := @OCAMLTOP@

INCLUDE = `$(OCAMLC) -where`
COBJS=init.o comm.o msgs.o collcomm.o groups.o utils.o
OBJS=mpi.cmo

CCOPT=-cc $(CC) -cclib "$(LIBS) $(LDFLAGS)" 

all: libcamlmpi.a mpi.cma mpi.cmxa 

install:
	cp mpi.mli mpi.cmi mpi.cma mpi.cmxa mpi.a libcamlmpi.a $(DESTDIRMPI)

libcamlmpi.a: $(COBJS)
	rm -f $@
	ar rc $@ $(COBJS)

mpi.cma: $(OBJS)
	$(OCAMLC) -cc $(CC) -a -o mpi.cma $(CCOPT) -custom $(OBJS) -cclib -lcamlmpi 

mpi.cmxa: $(OBJS:.cmo=.cmx)
	$(OCAMLOPT) -cc $(CC) -ccopt "$(CFLAGS)" -a -o mpi.cmxa $(CCOPT) $(OBJS:.cmo=.cmx) -cclib -lcamlmpi 

.SUFFIXES: .ml .mli .cmo .cmi .cmx

.ml.cmo:
	$(OCAMLC) -cc $(CC) -ccopt "$(CFLAGS)" $(CCOPT) -c $<
.mli.cmi:
	$(OCAMLC) -cc $(CC) $(CCOPT) -ccopt "$(CFLAGS)" -c $<
.ml.cmx:
	$(OCAMLOPT) -cc $(CC) $(CCOPT) -ccopt "$(CFLAGS)" -c $<
.c.o:
	$(OCAMLOPT) -cc $(CC) -ccopt "$(CFLAGS)" $(CCOPT) -c -I $(INCLUDE) $< $(PROFILE)

testmpi: test.ml mpi.cma libcamlmpi.a
	ocamlc -o testmpi unix.cma mpi.cma test.ml -ccopt -L.

clean::
	rm -f testmpi

test: testmpi
	mpirun -np 5 ./testmpi

test_mandel: test_mandel.ml mpi.cmxa libcamlmpi.a
	ocamlopt -o test_mandel graphics.cmxa mpi.cmxa test_mandel.ml -ccopt -L.

clean::
	rm -f test_mandel

clean::
	rm -f *.cm* *.o *.a

distclean: clean
	rm -f Makefile config.h config.log config.status

depend:
	$(OCAMLDEP) *.ml > .depend
	gcc -MM $(CFLAGS) *.c >> .depend

tol: 
	ocamlopt.opt -inline 500 -I native -c tol.ml
	cp tol.cmi ../
	cp tol.cmx ../
	cp tol.o ../

include .depend

